package cn.kgc.controller;

import cn.kgc.entity.Emp;
import cn.kgc.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class EmpConTroller {

    @Autowired
    private EmpService empService;
//查询
@GetMapping("/emp/{pageNo}/{pageSize}")
public String getAllEmp(@PathVariable("pageNo") Integer pageNo,
                        @PathVariable("pageSize") Integer pageSize,
                        Model model
){
    List<Emp> empList=empService.getAllEmp(pageNo,pageSize);
    model.addAttribute("empList",empList);
    return  "empList";
}
    //查询
    @GetMapping("/emp/{id}")
    public String getEmpById(@PathVariable("id")Integer id, Model model){
       Emp emp=empService.getEmpById(id);
        model.addAttribute("emp",emp);
        return "emp";

}
//增加
@GetMapping("/emp/ui")
public String getUI(Model model){
        model.addAttribute("emp",new Emp());
        return "emp";
}
    @GetMapping("/emp/name/{name}")
    @ResponseBody
    public Map<String,Object> countEmpByName(@PathVariable("name") String name){
        Integer count = empService.countEmpByName(name);
        Map<String,Object> data  = new HashMap<>();
        data.put("count",count);
        return data;
    }

    @PostMapping("/emp")
    public String addEmp(Emp emp){
       empService.addEmp(emp);
        return "redirect:/emp";
    }
    @PutMapping("/emp")
    public String updateEmp(Emp emp){
        empService.updateEmp(emp);
        return "redirect:/emp";
    }
    @DeleteMapping("/emp/{id}")
    public String deleteEmpById(@PathVariable("id")Integer id){
        empService.deleteEmpById(id);
        return "redirect:/emp";
    }


}
